/*
 * @Description:
 * @Author: wlj
 * @Date: 2022-12-22 16:25:32
 * @LastEditors: wlj
 * @LastEditTime: 2023-01-03 18:25:02
 * @FilePath: \my-vite-app\vite.config.ts
 */
import { defineConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import path from "path";
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import {
  // ElementPlusResolver,
  AntDesignVueResolver,
} from "unplugin-vue-components/resolvers";

// https://vitejs.dev/config/
export default defineConfig(({ command, mode }): any => {
  let envConfig = loadEnv(mode, process.cwd());
  console.log(envConfig);

  const isProduction = envConfig.VITE_NODE_ENV === "production";
  let proxyUrl = envConfig.VITE_APP_PROXY_HOST;
  return {
    root: process.cwd(), //项目根目录（index.html 文件所在的位置） 默认： process.cwd()
    base: "./", // 类似publicPath，'./'避免打包访问后空白页面，要加上，不然线上也访问不了
    // productionSourceMap: !isProduction,
    plugins: [
      vue(),
      AutoImport({
        imports: [
          "vue",
          "vue-router",
          "pinia",
          {
            axios: [["default", "axios"]],
          },
        ], // 自动导入vue和vue-router相关函数
        dts: "src/auto-import.d.ts", // 生成 `auto-import.d.ts` 全局声明
        resolvers: [AntDesignVueResolver()],
      }),
      Components({
        dts: true,
        dirs: ["src/components/global"], // 按需加载的文件夹
        resolvers: [
          // ElementPlusResolver(), // ElementPlus按需加载
          AntDesignVueResolver({
            resolveIcons: true, // 自动引入 ant-design/icons-vue中的图标
            importStyle: "less", //修改antdv主题色
          }),
        ],
      }),
    ],
    resolve: {
      alias: {
        "@": path.resolve(__dirname, "src"),
        "@assets": path.resolve(__dirname, "src/assets"),
        "@components": path.resolve(__dirname, "src/components"),
        "@images": path.resolve(__dirname, "src/assets/images"),
        "@views": path.resolve(__dirname, "src/views"),
        "@store": path.resolve(__dirname, "src/store"),
      },
    },
    css: {
      devSourcemap: !isProduction,
      preprocessorOptions: {
        less: {
          javascriptEnabled: true,
          // 全局变量使用：@primary-color
          modifyVars: {
            // hack: `true; @import (reference) "${path.resolve(
            //   "src/assets/less/var.less"
            // )}";`,
            // "primary-color": "#1890ff", // 全局主色
            // "link-color": " #1890ff", // 链接色
            // "success-color": " #52c41a", // 成功色
            // "warning-color": " #faad14", // 警告色
            // "error-color": " #f5222d", // 错误色
            // "font-size-base": " 14px", // 主字号
            // "heading-color": " rgba(0, 0, 0, 0.85)", // 标题色
            // "text-color": " rgba(0, 0, 0, 0.65)", // 主文本色
            // "text-color-secondary": " rgba(0, 0, 0, 0.45)", // 次文本色
            // "disabled-color": " rgba(0, 0, 0, 0.25)", // 失效色
            // "border-radius-base": " 2px", // 组件/浮层圆角
            // "border-color-base": " #d9d9d9", // 边框色
            // "box-shadow-base": " 0 2px 8px rgba(0, 0, 0, 0.15)", // 浮层阴影
          },
        },
      },
    },
    build: {
      outDir: "dist",
      assetsDir: "assets",
      sourcemap: false,
      minify: "terser", // 混淆器，terser 构建后文件体积更小，'terser' | 'esbuild'
      chunkSizeWarningLimit: 1500, // chunk大小警告的限制，默认500k
      terserOptions: {
        // 生产环境移除console
        compress: {
          drop_console: true,
          drop_debugger: true,
        },
        output: {
          comments: true, // 去掉注释内容
        },
      },
    },
    server: {
      https: false, // 是否开启https
      open: true, // 是否自动在浏览器打开
      cors: true, // 是否允许跨域
      port: Number(envConfig.VITE_APP_PORT),
      host: "0.0.0.0",
      proxy: {
        "/dev-api": {
          target: proxyUrl, // 后台接口
          changeOrigin: true,
          secure: false, // 如果是https接口，需要配置这个参数
          // ws: true, // websocket支持
          rewrite: (path) => path.replace(/^\/dev-api/, ""),
        },
      },
    },
    // 引入第三方配置
    optimizeDeps: {
      include: [],
    },
  };
});
